package cn.edu.zafu.bigdata.lb;

import java.io.*;

/**
 * @author LiuBo
 */
public class RhymingWords {
    public static void main(String[] args) {
        try {
            // 从文件中读取单词
            Reader words = new FileReader("files/words.txt");
            // 进行单词的逆序、排序、再逆序还原
            Reader rhymedWords = reverse(sort(reverse(words)));
            // 将处理后的单词列表输出显示
            BufferedReader in = new BufferedReader(rhymedWords);
            String input;
            while ((input = in.readLine()) != null) {
                System.out.println(input);
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 创建管道，创建并启动单词逆序线程
    public static Reader reverse(Reader source) throws IOException {
        PipedWriter pipeOut = new PipedWriter();
        PipedReader pipeIn = new PipedReader(pipeOut);
        PrintWriter out = new PrintWriter(pipeOut);
        new ReverseThread(out, source).start();
        return pipeIn;
    }

    // 创建管道，创建并启动单词排序线程
    public static Reader sort(Reader source) throws IOException {
        PipedWriter pipeOut = new PipedWriter();
        PipedReader pipeIn = new PipedReader(pipeOut);
        PrintWriter out = new PrintWriter(pipeOut);
        new SortThread(out, source).start();
        return pipeIn;
    }
}